TOKENS in C

 TOKENS:

They are the smallest individual unit that we give in the program.

They are 

Keywords
Identifiers
Constants/literals
Strings

Operators

Keywords:


The meanings of keywords have been set as fixed and cannot be altered.
In C, there are 32 keywords in all.
Lowercase letters are used to write keywords.
For example, int, double.


 Identifiers


identifiers are the names that the users provide to each elements of the program.

 It refers to the name of functions, variables and arrays. For the identifiers we follow the followings rules:

  • An underscore or a letter must be the first character.
  • It must be formed by underscore, numeral or letter.
  • The identifier must not be a keyword.
  • It does not contain any blank or white space characters.
  • It should be meaningful.

 Examples of valid identifiers
sum_1,  result



Constants
Constants are fixed values that remain the same while a program is running.
 Different kinds of constants

Integer Constants

 All that an integer constant is is a value made up of digits or numbers. For instance, 111, 1234


 Floating point Constants

 Constants that have a fraction value or a decimal point in them. For instance, 223.14, 400.054


 Character Constants

 A character constant is made up of just one character and one quote (' '). Such as "A," "m," and "9."


Strings

All strings are is a character array terminated with the null character ('\0').

 Double quotes are always used to enclose strings.

 For instance, "Welcome"

Operators

 The operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations

 Example: +, -, /, *, ++, --

Types of ‘C’ operators:

1. Arithmetic operators

2. Relational operators

3. Logical operators

4. Assignment operators

5. Increment and Decrement operators

6. Conditional operators

7. Bitwise operators

8. Special operators


Arithmetic Operators

Arithmetic operators are used for performing basic mathematical operations on operands.


Operator

         Name

         Description

Example     

+

         Addition

         Adds together two values

         x + y

-

         Subtraction

         Subtracts one value from another

         x - y 

*

         Multiplication

         Multiplies two values

         x * y 

/

         Division

         Divides one value by another

         x / y 

%

         Modulus

         Returns the division remainder

         x % y

++

         Increment

Increases the value of a variable by 1

         ++x 

--

         Decrement

         Decreases the value of a variable by 1

         --x   



Example Program
// Working of arithmetic operators

#include <stdio.h>

int main()

{

    int a = 9,b = 4;  

printf("%d ", ++a);  //pre-increment //10

printf("\n %d ", a++);  //post increment 10

printf("\n %d ", a);  //11

    

    printf("\n a+b = %d \n",a+b); //11+4

     

    printf("a-b = %d \n",a-b); //11-4

    

    printf("a*b = %d \n",a*b); //11*4

    

    printf("a/b = %d \n",a/b); //11/4 (quotient)

    

    printf("Remainder when a divided by b = %d \n",a%b); //11%4 (remanider)

    

    printf("%d ", --b); //4

printf("%d ", b--);

printf("%d ", b); 

    return 0;

}


// Working of increment and decrement operators

#include <stdio.h>

int main()

{

    int a,b,c,d;

    a=30;

    b=100;

    c=40;

    d=26;

    


    printf("++a = %d \n", ++a);

    printf("--b = %d \n", --b);

    printf("c++ = %d \n", c++);

    printf("%d \n", c);

    printf(" --d = %d \n", d--);

    printf("%d", d);


    return 0;

}



Relational Operators

Relational or comparison operators are used to compare two operands.

The result of the evaluation is either true or false.

 

Operator

Name

Example

==

Equal to

x == y

!=

Not equal

x != y

> 

Greater than

x > y

< 

Less than

x < y

>=

Greater than or equal to

x >= y

<=

Less than or equal to

x <= y



Example Program

// Working of relational operators true = 1 false = 0

#include <stdio.h>

int main()

{

    int a = 5, b = 5, c = 10;


    printf("%d == %d is %d \n", a, b, a == b); //1

    printf("%d == %d is %d \n", a, c, a == c); //0

    printf("%d > %d is %d \n", a, b, a > b); //0

    printf("%d > %d is %d \n", a, c, a > c); //0

    printf("%d < %d is %d \n", a, b, a < b); //0

    printf("%d < %d is %d \n", a, c, a < c); //1

    printf("%d != %d is %d \n", a, b, a != b); //0

    printf("%d != %d is %d \n", a, c, a != c); //1

    printf("%d >= %d is %d \n", a, b, a >= b); //1

    printf("%d >= %d is %d \n", a, c, a >= c); //0

    printf("%d <= %d is %d \n", a, b, a <= b); //1

    printf("%d <= %d is %d \n", a, c, a <= c); //1


    return 0;

}


Logical Operators

«Logical operators are used for evaluating a combination of conditions/constraints

to get a resultant value.

«The result of the evaluation of a Boolean expression is Boolean which is either

true or false.



Operator

Name

Description

Example

&&

Logical and

Returns true if both statements are true

x < 5 &&  x < 10

||

Logical or

Returns true if one of the statements is true

x < 5 || x < 4

!

Logical not

Reverse the result, returns false if the result is true

!(x < 5 && x < 10)


Example Program

// Working of logical operators true =1 false = 0

#include <stdio.h>
int main()
{
    int a = 5, b = 5, c = 10, result;

    result = (a == b) && (c > b); //1
    printf("(a == b) && (c > b) is %d \n", result);

    result = (a == b) && (c < b); //0
    printf("(a == b) && (c < b) is %d \n", result);

    result = (a == b) || (c < b); //1
    printf("(a == b) || (c < b) is %d \n", result);

    result = (a != b) || (c < b); //0
    printf("(a != b) || (c < b) is %d \n", result);

    result = !(a != b); //1
    printf("!(a != b) is %d \n", result);

    result = !(a == b); //0
    printf("!(a == b) is %d \n", result);

    return 0;
}

Assignment operators
«Assignment operators are used to assigning value to a variable.
« “=".
This is the simplest assignment operator.
This operator is used to assign the
value on the right to the variable on the left.
For example:
a = 10;
b = 20;
ch = y';

 

Operator

Example

Same As

=

x = 5

x = 5

+=

x += 3

x = x + 3

-=

x -= 3

x = x - 3

*=

x *= 3

x = x * 3

/=

x /= 3

x = x / 3

%=

x %= 3

x = x % 3

&=

x &= 3

x = x & 3

|=

x |= 3

x = x | 3

^=

x ^= 3

x = x ^ 3

>>=

x >>= 3

x = x >> 3

<<=

x <<= 3

x = x << 3


Example Program:

// Working of assignment operator = 

#include <stdio.h>

int main()

{

    int a = 5, c=20;


    c += a;  //c=c+a; 25   

    printf("c = %d\n", c);

    c -= a;   //20

    printf("c = %d\n", c);

    c *= a;     //100

    printf("c = %d\n", c);

    c /= a;     //20

    printf("c = %d\n", c);

    c %= a;    //0

    printf("c = %d\n", c);


    return 0;

}

Bitwise Operators


In C, bitwise operators perform operations on integer data at the

individual bit-level.


& (bitwise and)

| (bitwise or)

<< (bitwise shift left)

>> (bitwise shift right)


Example Program:

/*
Bitwise AND operator is denoted by the single ampersand sign (&).
Two integer operands are written on both sides of the (&) operator.
If the corresponding bits of both the operands are 1,
then the output of the bitwise AND operation is 1; otherwise, the output would be 0.

For example,

We have two variables a and b.
a =6;
b=4;
The binary representation of the above two variables are given below:
a = 0110
b = 0100
When we apply the bitwise AND operation in the above two variables, i.e., a&b, the output would be:
Result = 0100  */

#include <stdio.h>
int main() {
int a=6, b=4;  // variable declarations
printf("The output of the Bitwise AND operator a&b is %d",a&b);
return 0;
}

/*The bitwise complement operator is also known as one's complement operator.
It is represented by the symbol tilde (~).
It takes only one operand or variable and performs complement operation on an operand.
When we apply the complement operation on any bits, then 0 becomes 1 and 1 becomes 0.
If we have a variable named 'a',
a = 8;
The binary representation of the above variable is given below:
a = 1000
When we apply the bitwise complement operator to the operand, then the output would be:
Result = 0111  */

#include <stdio.h>
int main() {
int a=8;  // variable declarations
printf("The output of the Bitwise complement operator ~a is %d", ~a);
return 0;
}

/*
Left-shift operator

It is an operator that shifts the number of bits to the left-side.
syntax: Operand << n
Suppose we have a statement:  
int a = 5;  
The binary representation of 'a' is given below:  
a = 0101  
If we want to left-shift the above representation by 2, then the statement would be:   
a << 2;  
0101<<2 = 010100    
*/



#include <stdio.h>  
int main()  
{  
    int a=5; // variable initialization  
    printf("The value of a<<2 is : %d ", a<<2);  
    return 0;  
}  

/*
The bitwise OR operator is represented by a single vertical sign (|).
Two integer operands are written on both sides of the (|) symbol.
If the bit value of any of the operand is 1,
then the output would be 1, otherwise 0.

For example,

We consider two variables,
a = 23;
b = 10;
The binary representation of the above two variables would be:
a = 0001 0111
b = 0000 1010
When we apply the bitwise OR operator in the above two variables, i.e., a|b , then the output would be:
Result = 0001 1111  */



#include <stdio.h>
int main() {
int a=23,b=10;  // variable declarations
printf("The output of the Bitwise OR operator a|b is %d",a|b);
return 0;
}

/*
It is an operator that shifts the number of bits to the right side.

Syntax of the right-shift operator is given below:

syntax: Operand >> n;

int a = 7;  
The binary representation of the above variable would be:  
a = 0111  
If we want to right-shift the above representation by 2, then the statement would be:  
a>>2;  
0111 >> 2 = 01    
*/


#include <stdio.h>  
int main()  
{  
    int a=7; // variable initialization  
    printf("The value of a>>2 is : %d ", a>>2);  
    return 0;  
}  

/*
Bitwise exclusive OR operator is denoted by (^) symbol. 
Two operands are written on both sides of the exclusive OR operator. 
If the corresponding bit of any of the operand is 1 then the output would be 1, 
otherwise 0.

For example, We consider two variables a and b,  
a = 12;  
b = 10;  
The binary representation of the above two variables would be:  
a = 0000 1100  
b = 0000 1010  
When we apply the bitwise exclusive OR operator in the above two variables (a^b), then the result would be:  
Result = 0000 1110  */



#include <stdio.h>  
int main()  
{  
   int a=12,b=10;  // variable declarations  
   printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);  
   return 0;  
}  

Conditional operator
‘The conditional operator is also known as a temary operator. The conditional statements are the decision-making statements
‘which depends upon the output of the expression. It is represented by two symbols, i.e.” ? : "
As conditional operator works on three operands, so itis also known as the temary operator.
The behavior of the conditional operator is similar to the ‘if-else’ statement as ‘if-else’ statement is also a decision-making
statement.


Example Program:


/*Conditional operator

syntax ?:(if else replacement)*/


#include<stdio.h>


int main() {

printf("\tA program to check whether the given number is even number or odd number\n");

int number;

printf("Enter the number: ");

scanf("%d",&number);


printf((number%2==0)?"\tThe given number is even":"\tThe given number is odd");


}


Punctuators


 (,) Comma - Separator 

 (.) full stop - used to call a function

 () Parentheses - defines a function

 Curly braces {} - Specifies a block

(*) asterisk - Pointer 


Post a Comment

0 Comments